import axios from 'axios';
import * as qs from 'query-string';

import {Inject} from '../router';
import {API_URL} from "../constants";


class HttpTools {
    private $http;

    constructor() {
        this.$http = axios.create({
            baseURL: API_URL
        });
    }

    public get(url, data?: any, options?: any) {
        let axiosOptions = {};

        if (data) {
            axiosOptions = {params: data}
        }

        if (options) {
            axiosOptions = {...options};
        }

        return this.$http.get(url, axiosOptions);
    }

    public post(url, data?: any, options?: any) {
        let axiosOptions = {};
        if (options) {
            axiosOptions = {...options};
        }

        return this.$http.post(url, data, axiosOptions);
    }

    public put(url, data, options?: any) {
        let axiosOptions = {};
        if (options) {
            axiosOptions = {...options};
        }

        return this.$http.put(url, data, axiosOptions);
    }

    public delete(url, options?: any) {
        return this.$http.get(url, options);
    }
}

export const Http = () => {
    return Inject(() => {
        return new HttpTools();
    });
};
